package com.oocl.im.gui;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oocl.im.pojo.MsgPackage;
import com.oocl.im.pojo.Operation;
import com.oocl.im.util.JsonUtil;

public class LoginFrame extends JFrame {
	private JTextField nameTf;
	private JPasswordField psdPf;
	private JButton registerBtn;
	private JButton loginBtn;
	private JButton cancelBtn;
	private JLabel userName;
	private JLabel password;

	public LoginFrame() throws HeadlessException {
		super();
		this.setSize(400, 260);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		init();
		addEvent();
		this.setVisible(true);
		this.setTitle("Welcome to login !");
	}

	private void init() {
		this.setLayout(null);
		nameTf = new JTextField();
		psdPf = new JPasswordField();
		loginBtn = new JButton("login");
		registerBtn = new JButton("register");
		cancelBtn = new JButton("cancel");
		userName = new JLabel("UserName:");
		password = new JLabel("Password:");
		nameTf.setBounds(120, 20, 230, 35);
		psdPf.setBounds(120, 80, 230, 35);
		registerBtn.setBounds(60, 160, 80, 40);
		loginBtn.setBounds(150, 160, 80, 40);
		cancelBtn.setBounds(240, 160, 80, 40);
		userName.setBounds(30, 20, 90, 35);
		password.setBounds(30, 80, 90, 35);
		this.add(nameTf);
		this.add(psdPf);
		this.add(loginBtn);
		this.add(cancelBtn);
		this.add(userName);
		this.add(password);
		this.add(registerBtn);
	}

	private void addEvent() {
		registerBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new RegisterFrame();
				dispose();
			}
		});
		loginBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					String name = nameTf.getText();
					String password = psdPf.getText();
					Socket socket = new Socket("127.0.0.1", 8123);
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(socket.getInputStream()));
					PrintWriter writer = new PrintWriter(socket
							.getOutputStream(), true);
					MsgPackage msg = new MsgPackage(Operation.LOGIN, name,
							null, password, null, null);
					System.out.println(msg);
					String msgJson = JsonUtil.objectToString(msg);
					writer.println(msgJson);
					String msgJson2 = reader.readLine();
					MsgPackage msg2 = JsonUtil.stringToObject(msgJson2);
					System.out.println(msg2);
					switch (msg2.getOpeartion()) {
					case EXIT:
						new InfoFrame(msg2.getContent());
						socket.close();
						break;
					case LOGIN:
						new ChatFrame(name, socket);
						dispose();
						break;
					default:
						break;
					}
				} catch (UnknownHostException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					//new InfoFrame("Server isn't open !");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					//e1.printStackTrace();
					new InfoFrame("Server isn't open !");
				}
			}
		});
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("cancel login...");
				System.exit(0);

			}
		});
	}

	public static void main(String[] args) {
		new LoginFrame();
	}
}
